package hu.kisprof.quiz.view.dialogs;

import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class QuizChallengeSentDialog extends JDialog implements ActionListener, Runnable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -851266300554068962L;
	private JTextField chlStateF;
	private JPanel holdingPanel;
	private QuizGUI gui;
	private String username;
	private JButton ok;
	private String message;
	private int difficulty;
	
	
	public QuizChallengeSentDialog(String username, QuizGUI gui, String message, int difficulty){
		super(gui.getWindow(),Labels.challengePlayer,false);
		this.gui=gui;
		this.username=username;
		this.message = message;
		this.difficulty = difficulty;
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		chlStateF = new JTextField(Labels.unknown);
		chlStateF.setBackground(Color.ORANGE);
		chlStateF.setEditable(false);
		chlStateF.setFont(new Font(chlStateF.getFont().getFontName(),Font.BOLD,16));
		
		holdingPanel = new JPanel(new BorderLayout());
		holdingPanel.add(new JLabel(username+" "+Labels.plyChlState+":          "), BorderLayout.NORTH);
		JPanel tmpPanel = new JPanel(new GridLayout(1,1));
		tmpPanel.add(chlStateF);
		holdingPanel.add(tmpPanel,BorderLayout.CENTER);
		
		ok = new JButton(Labels.ok);
		ok.setEnabled(false);
		ok.addActionListener(this);
		
		holdingPanel.add(ok,BorderLayout.SOUTH);
		
		
		
		getContentPane().add(holdingPanel);
        //setPreferredSize(new Dimension(200,140));
        this.pack();
        this.setResizable(false);
        setLocationRelativeTo(gui.getWindow());
        new Thread(this).start();
        setVisible(true);
		
		
		
	}
	
	
	public void updateStatus(boolean status){
		if(status){
			chlStateF.setText(Labels.accepted);
			chlStateF.setBackground(Color.green);
		}else{
			chlStateF.setText(Labels.declined);
			chlStateF.setBackground(Color.red);
		
		}
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		ok.setEnabled(true);
		this.pack();
		holdingPanel.validate();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(Labels.ok)&&chlStateF.getText().equals(Labels.accepted)){
			gui.showGamePanel();
			gui.getControl().setInGame(username, true);
			this.setVisible(false);
			gui.getControl().playerReady(true);
		}
		if(e.getActionCommand().equals(Labels.ok)&&chlStateF.getText().equals(Labels.declined)){
			this.setVisible(false);
		}
		
	}


	@Override
	public void run() {
		updateStatus(gui.getControl().challangePlayer(username,message,difficulty));
		
	}
}
